package com.xuxin.baby.fragment;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.xuxin.baby.R;
import com.xuxin.baby.base.BaseConstant;
import com.xuxin.baby.base.BaseFragment;
import com.xuxin.baby.bean.BaseBean;
import com.xuxin.baby.contract.ChildEatingRecordContract;
import com.xuxin.baby.event.BaseEventFragment;
import com.xuxin.baby.event.EatingBreastEvent;
import com.xuxin.baby.presenter.ChildEatingRecordPresenter;
import com.xuxin.baby.utils.ACache;
import com.xuxin.baby.utils.DatePickerUtils;
import com.xuxin.baby.view.TimeView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xu_xin
 * Created on 2020.08.17 0017
 * 记喂养 -> 母乳
 */
public class ChildEatingBreastFragment extends BaseEventFragment implements ChildEatingRecordContract.View {
    private final ChildEatingRecordContract.Presenter presenter = new ChildEatingRecordPresenter(this);
    @BindView(R.id.child_eating_breast_timer)
    TimeView child_eating_breast_timer;
    @BindView(R.id.child_eating_breast_record)
    TextView child_eating_breast_record;//计时按钮
    @BindView(R.id.child_eating_breast_start)
    TextView child_eating_breast_start;//母乳开始
    @BindView(R.id.child_eating_breast_end)
    TextView child_eating_breast_end;//母乳结束

    private ACache aCache;
    private String tenantId;//租户id
    private String token;//授权token
    private String childId;//宝宝ID
    private float feedTimes;//母乳时间

    public static ChildEatingBreastFragment newInstance() {
        return new ChildEatingBreastFragment();
    }

    @Override
    public View setContentView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
        return inflater.inflate(R.layout.fragment_child_eating_breast, container, false);
    }

    @Override
    public void initView() {
    }

    @Override
    public void initData() {
        aCache = ACache.get(getContext());
        tenantId = aCache.getAsString(BaseConstant.tenantId);
        token = aCache.getAsString(BaseConstant.token);
        childId = aCache.getAsString(BaseConstant.childId);
        Long time = (Long) aCache.getAsObject(BaseConstant.breastRecord);
        if (null != time) {
            child_eating_breast_start.setText(TimeUtils.millis2String(time));
            child_eating_breast_record.setText("结束计时");
            child_eating_breast_timer.startTimer(time);
        } else {
            child_eating_breast_record.setText("开始计时");
        }
    }

    @Override
    public void onComplete() {
        dismiss();
    }

    @Override
    @OnClick({R.id.child_eating_breast_record, R.id.child_eating_breast_start,
            R.id.child_eating_breast_end})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.child_eating_breast_record) {
            String record = child_eating_breast_record.getText().toString();
            long time = System.currentTimeMillis();
            if ("开始计时".equals(record)) {
                aCache.put(BaseConstant.breastRecord, time);
                child_eating_breast_start.setText(TimeUtils.millis2String(time));
                child_eating_breast_end.setText("请选择时间");
                child_eating_breast_record.setText("结束计时");
                child_eating_breast_timer.startTimer(time);
            } else {
                child_eating_breast_end.setText(TimeUtils.millis2String(time));
                child_eating_breast_record.setText("开始计时");
                aCache.remove(BaseConstant.breastRecord);
                child_eating_breast_timer.stopTimer();
            }
        } else if (id == R.id.child_eating_breast_start) {
            DatePickerUtils.showTodayTime(getContext(), new DatePickerUtils.onListener() {
                @Override
                public void onShow(String time) {
                    child_eating_breast_start.setText(time);
                    child_eating_breast_timer.stopTimer();
                    child_eating_breast_record.setText("开始计时");
                    aCache.remove(BaseConstant.breastRecord);
                }
            });
        } else if (id == R.id.child_eating_breast_end) {
            DatePickerUtils.showTodayTime(getContext(), new DatePickerUtils.onListener() {
                @Override
                public void onShow(String time) {
                    child_eating_breast_end.setText(time);
                    child_eating_breast_timer.stopTimer();
                    child_eating_breast_record.setText("开始计时");
                    aCache.remove(BaseConstant.breastRecord);
                }
            });
        }
    }

    private final Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what == 0) {
                presenter.recordBreast(tenantId, token, childId, TimeUtils.getNowString(), feedTimes);
            }
            return false;
        }
    });

    @Override
    public void onSuccess(BaseBean baseBean) {
        requireActivity().finish();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(EatingBreastEvent event) {
        try {
            String startTime = child_eating_breast_start.getText().toString();
            String endTime = child_eating_breast_end.getText().toString();
            long second = (TimeUtils.string2Millis(endTime) - TimeUtils.string2Millis(startTime)) / 1000;
            if (StringUtils.isEmpty(startTime)) {
                ToastUtils.showShort("请选择开始时间");
            } else if (StringUtils.isEmpty(endTime)) {
                ToastUtils.showShort("请选择结束时间");
            } else if (second <= 0 || second >= 86400) {
                ToastUtils.showShort("时间选择有误");
            } else {
                LogUtils.e("记喂养");
                feedTimes = (float) (second / 60.0);
                popup.show();
                handler.sendEmptyMessage(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
            dismiss();
        }
    }
}